package com.tedu.excelinportdamo.common.aspect;


import com.tedu.excelinportdamo.common.result.JsonResult;
import com.tedu.excelinportdamo.common.result.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Aspect
@Slf4j
public class ExcelFileAspect {


    @Before("execution(public com.tedu.excelinportdamo.common.result.JsonResult com.tedu.excelinportdamo.controller.NodeController.handleFileUpload(..))")
    public void checkFile(JoinPoint joinPoint) throws IOException {
        log.info("进入切面,判断文件格式");
        // 获取文件对象
        MultipartFile file=(MultipartFile) joinPoint.getArgs()[0];

        if (file == null || file.isEmpty()) {
            log.info("文件为空");
            throw new RuntimeException("文件为空");
        }
        if (!file.getOriginalFilename().toLowerCase().endsWith(".xlsx")){
            // 文件格式错误
            log.info("文件格式错误");
            throw new RuntimeException("文件格式错误");
        }

    }
}
